<template>
  <com-card>
    <div slot="com_header">
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入商品名称"
            v-model="goodsObj.query"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="query"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4" style="margin-left: 20px">
          <el-button @click="jump" type="primary" icon="el-icon-plus"
            >添加</el-button
          >
        </el-col>
      </el-row>
    </div>
    <div slot="com_content">
      <el-row>
        <el-table :data="tableData" border>
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column prop="goods_name" label="商品名称" width="300px">
          </el-table-column>
          <el-table-column prop="goods_price" label="商品价格">
          </el-table-column>
          <el-table-column prop="goods_number" label="商品数量">
          </el-table-column>
          <el-table-column prop="add_time" label="创建时间"> </el-table-column>
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-tooltip content="修改商品" placement="top">
                <el-button
                  size="small"
                  type="primary"
                  icon="el-icon-edit"
                  @click="xgquery(row)"
                ></el-button>
              </el-tooltip>
              <el-tooltip content="删除商品" placement="top">
                <el-button
                  size="small"
                  type="danger"
                  icon="el-icon-delete"
                  @click="del(row.goods_id)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="goodsObj.pagenum"
          :page-sizes="[3, 5, 7, 9]"
          :page-size="goodsObj.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
      <!-- 修改弹窗 -->
      <el-dialog title="提示" :visible.sync="dialogbj" width="30%">
        <el-form :model="xgobj" label-width="70px">
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="xgobj.goods_name"></el-input>
          </el-form-item>

          <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model="xgobj.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number">
            <el-input v-model="xgobj.goods_number"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogbj = false">取 消</el-button>
          <el-button type="primary" @click="deleng()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </com-card>
</template>

<script>
import ComCard from "../../components/comCard";
import { api_goods, api_goodsadd, api_delete } from "../../api/goods.js";
export default {
  components: {
    ComCard,
  },
  data() {
    return {
      goodsObj: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      //总数据列表
      tableData: [],
      //总数据条数
      total: 0,
      //修改
      xgobj: {},
      //修改弹窗
      dialogbj: false,
    };
  },
  mounted() {
    this.query();
  },
  methods: {
    //查询
    async query() {
      let rst = await api_goods(this.goodsObj);
      console.log(rst);
      let { total, goods } = rst.data;
      this.total = total;
      this.tableData = goods;
    },
    //当页发生变化触发的函数
    handleCurrentChange(pagenum) {
      this.goodsObj.pagenum = pagenum;
      this.query();
    },
    //当条数发生变化触发的函数
    handleSizeChange(pagesize) {
      this.goodsObj.pagesize = pagesize;
      this.query();
    },

    //添加按钮跳转
    jump() {
      this.$router.push("./goodsJump");
    },
    //修改
    xgquery(row) {
      this.dialogbj = true;
      let { id, goods_name, goods_price, goods_number } = row;
      this.xgobj = { id, goods_name, goods_price, goods_number };
    },
    //修改弹窗确定按钮
    async deleng() {
      let rst = await api_goodsadd(this.xgobj);
      console.log(rst);
    },
    //删除
    del(goods_id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(
        async () => {
          let rst = await api_delete({ url: goods_id });
          let { status, msg } = rst.meta;
          if (status == 200) {
            this.$message.success(msg);
            this.query();
          }
        },
        () => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        }
      );
    },
  },
};
</script>

<style lang="less" scoped>
</style>